public class Solution {
    public static void main(String[] args) {
        int [] arr = {1,3} ;
        System.out.println(lastStoneWeight(arr));
    }
    public static int lastStoneWeight(int[] stones) {
       int len = stones.length ;
       while(len > 1){
         int x = max(stones,len) ;
         stones[x] = 0 ;
         int y = max(stones,len) ;
         if(stones[x] > stones[y]){
          stones[x] = stones[x] - stones[y] ;
         yd(y ,len, stones) ;
         len -- ;
         }else{
           if(x > y){
               yd(x,len,stones) ;
               yd(y,len,stones) ;
           }else{
               yd(y,len,stones) ;
               yd(x,len,stones) ;
           }
           len = len - 2 ;
         }
       }
      return stones[0] ;
    
    }
    public static int max(int[] array , int len){
           
           int n = 0 ;
       for(int i = 1 ; i<len ;i++){
           if(array[n] < array[i]){
               n = i ;
           }
       }
         return n ;
    }

    public static void yd(int n  ,int len, int[] array){
      for(int i = n ; i < len-1 ;i ++){
           array[i] = array[i+1] ;
      }
    }

}
